package day_06;

/**
 * @ClassName binarySearch
 * @Description TODO
 * @Author hp
 * @Date 2021/6/8 14:30
 * @Version 1.0
 **/
public class binarySearch {
    public static void main(String[] args) {
        int[] arr = new int[]{4, 7, 12, 56, 89, 100};
        int target = 89;
        int left = 0;
        int right = arr.length;

        int index = -1;
        while (left <= right) {
            int middle = (left + right) / 2;
            if (arr[middle] == target) {
                index = middle;
                System.out.println("arr = " + arr[index]);
                break;
            } else if (arr[middle] > target) {
                right = middle - 1;

            } else {
                left = middle + 1;

            }
        }
        if (index == -1) {
            System.out.println(target + "不存在");
        } else {
            System.out.println("找到了");
        }

    }
}
